<?php
function save2bet2( $uid, $acl, $pid, $bettype, $betcounts, $num, $money, $rate, $u2, $u3, $u4, $u5, $stime )
{
    global $lotto_id;
    global $prot_start;
    $fields = array();
    $fields['user_id'] = $uid;
    $fields['prev_id'] = $pid;
    $fields['acl'] = $acl;
    $fields['lotto_id'] = $lotto_id;
    $fields['createtime'] = $stime;
    $fields['bettype'] = $bettype;
    $fields['numbers'] = "'".$money."|".$rate."|".$betcounts."|".$num."'";
    $fields['amount'] = $money * $betcounts;
    $fields['line_nums'] = "'{$num}'";
    $fields['unitmoney'] = $money;
    if ( strpos( $rate, "," ) == 0 )
    {
        $fields['rate'] = $rate;
    }
    else
    {
        $p = explode( ",", $rate );
        $fields['rate'] = $p[0] - 0;
    }
    if ( $fields['rate'] <= 0 || $fields['amount'] <= 0 )
    {
        return 0 - 1;
    }
    $percent_sw = $u5['percent_sw'];
    $protmod = $u5['protmod'];
    if ( $acl == 2 )
    {
        $fields['uperid3'] = $u2['prev_id'];
        $fields['uperid4'] = $u3['prev_id'];
        $fields['uperid5'] = $u4['prev_id'];
        $fields['back2'] = GetBack( $u2['id'], $bettype, 1, 2 );
        $fields['back3'] = GetBack( $u3['id'], $bettype, 1, 3 );
        $fields['back4'] = GetBack( $u4['id'], $bettype, 1, 4 );
        $fields['back5'] = GetBack( $u5['id'], $bettype, 1, 5 );
        $fields['back6'] = GetBack( $u5['supe_account'], $bettype, 1, 255 );
        $fields['percent3'] = $u2['percent0'];
        $fields['percent4'] = $u3['percent0'];
        if ( $percent_sw == 1 )
        {
            $fields['percent5'] = $u5['mpercent'] - $fields['percent4'] - $fields['percent3'];
        }
        else
        {
            $fields['percent5'] = $u4['percent0'];
        }
        $fields['r2'] = $money * $fields['percent3'] / 100;
        $fields['r3'] = $money * $fields['percent4'] / 100;
        $fields['r4'] = $money * $fields['percent5'] / 100;
    }
    else if ( $acl == 3 )
    {
        $fields['uperid3'] = $uid;
        $fields['uperid4'] = $u3['prev_id'];
        $fields['uperid5'] = $u4['prev_id'];
        $fields['back2'] = 100;
        $fields['back3'] = GetBack( $u3['id'], $bettype, 1, 3 );
        $fields['back4'] = GetBack( $u4['id'], $bettype, 1, 4 );
        $fields['back5'] = GetBack( $u5['id'], $bettype, 1, 5 );
        $fields['back6'] = GetBack( $u5['supe_account'], $bettype, 1, 255 );
        $fields['percent3'] = 0;
        $fields['percent4'] = $u3['percent0'];
        if ( $percent_sw == 1 )
        {
            $fields['percent5'] = $u5['mpercent'] - $fields['percent4'];
        }
        else
        {
            $fields['percent5'] = $u4['percent0'];
        }
        $fields['r3'] = $money * $fields['percent4'] / 100;
        $fields['r4'] = $money * $fields['percent5'] / 100;
    }
    else if ( $acl == 4 )
    {
        $fields['uperid4'] = $uid;
        $fields['uperid5'] = $u4['prev_id'];
        $fields['back2'] = 100;
        $fields['back3'] = 100;
        $fields['back4'] = GetBack( $u4['id'], $bettype, 1, 4 );
        $fields['back5'] = GetBack( $u5['id'], $bettype, 1, 5 );
        $fields['back6'] = GetBack( $u5['supe_account'], $bettype, 1, 255 );
        $fields['percent3'] = 0;
        $fields['percent4'] = 0;
        if ( $percent_sw == 1 )
        {
            $fields['percent5'] = $u5['mpercent'];
        }
        else
        {
            $fields['percent5'] = $u4['percent0'];
        }
        $fields['r4'] = $money * $fields['percent5'] / 100;
    }
    else if ( $acl == 5 )
    {
        $fields['uperid5'] = $uid;
        $fields['back2'] = 100;
        $fields['back3'] = 100;
        $fields['back4'] = 100;
        $fields['back5'] = GetBack( $u5['id'], $bettype, 1, 5 );
        $fields['back6'] = GetBack( $u5['supe_account'], $bettype, 1, 255 );
        $fields['percent3'] = 0;
        $fields['percent4'] = 0;
        $fields['percent5'] = 0;
    }
    if ( $percent_sw == 1 )
    {
        $fields['percent255'] = 100 - $u5['mpercent'];
    }
    else
    {
        $fields['percent255'] = 100 - $fields['percent5'] - $fields['percent4'] - $fields['percent3'];
    }
    $fields['r6'] = $money * $fields['percent255'] / 100;
    $fields['betmode'] = 1;
    $fields['ip'] = "'".getIP( )."'";
    db_insert( "bet2", $fields );
    if ( $prot_start == 1 && $protmod == 1 )
    {
        if ( $acl == 2 )
        {
            auto_prots( 3, $bettype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $betcounts );
            auto_prots( 4, $bettype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $betcounts );
            auto_prots( 5, $bettype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $betcounts );
        }
        else
        {
            if ( $acl == 3 )
            {
                auto_prots( 4, $bettype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $betcounts );
                auto_prots( 5, $bettype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $betcounts );
            }
            else
            {
                if ( $acl == 4 )
                {
                    auto_prots( 5, $bettype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $betcounts );
                }
            }
        }
    }
}

echo "  ";
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
global $curruser;
$ui = new UI();
if ( $curruser['acl'] == 5 && $curruser['percent_sw'] == 1 )
{
    err( "你是固定占成的大股东，不能走飞！" );
}
$bettype = intval( $_POST['type'] );
$money = intval( $_POST['b23'] );
if ( $money == 0 )
{
    err( "错误的参数！" );
}
if ( $bettype == 29 )
{
    $count_num = 5;
}
elseif ( $bettype == 48 )
{
    $count_num = 4;
}
else
{
    $count_num = $bettype == 8 || $bettype == 9 || $bettype == 33 ? 3 : 2;
}
$num = array();
$numcount = 0;
$i = 1;
while ( $i < 50 )
{
    if ( isset( $_POST["num_".$i] ) )
    {
        $num[$numcount] = d2( $i );
        ++$numcount;
    }
    ++$i;
}
if ( $count_num == 5 )
{
    $betcounts = $numcount * ( $numcount - 1 ) * ( $numcount - 2 ) * ( $numcount - 3 ) * ( $numcount - 4 ) / 120;
}
elseif ( $count_num == 4 )
{
    $betcounts = $numcount * ( $numcount - 1 ) * ( $numcount - 2 ) * ( $numcount - 3 ) / 24;
}
else if ( $count_num == 2 )
{
    $betcounts = $numcount * ( $numcount - 1 ) / 2;
}
else
{
    $betcounts = $numcount * ( $numcount - 1 ) * ( $numcount - 2 ) / 6;
}
$betmoney = $money * $betcounts;
$conf = new Config();
$startbet = $conf->get( "startbet" );
$stopbet = $conf->get( "linebet" );
$prot_start = $conf->get( "protect_start" );
$betmin = $conf->get( "betmin" );
$auto_num = array();
$auto_down = 0;
$auto_count = 0;
$i = 0;
while ( $i < 10 )
{
    $nn = $conf->get( "autorate_b29_num".$i );
    if ( substr_count( $nn, "," ) == 4 )
    {
        $auto_num[$auto_count] = $nn;
        ++$auto_count;
    }
    ++$i;
}
$auto_down = $conf->get( "autorate_b29_down" );
if ( $curruser['enabled'] == 0 || $curruser['enabled'] == 2 )
{
    err( "帐号已被暂停使用！" );
}
$userlist = GetUserList( $curruser['id'] );
if ( $userlist == "" )
{
    $ui->showErrorPage( "<p>还没有建立会员！</p>" );
    exit();
}
$rs = getdb( "select * from lotto where open>='".date( "Y-m-d" )."' order by open" );
if ( $rs->eof )
{
    err( "目前没有可以下注期数！" );
}
$open = $rs->f['open'];
$lotto_id = $rs->f['id'];
$lotto_ns = $rs->f['n1'] + $rs->f['n2'] + $rs->f['n3'] + $rs->f['n4'] + $rs->f['n5'] + $rs->f['n6'];
if ( date( "Y-m-d" ) == $open )
{
    if ( date( "Hi" ) < $startbet )
    {
        err( "本期尚未开盘！" );
    }
    if ( $stopbet <= date( "Hi" ) || 0 < $lotto_ns )
    {
        err( "已经截止下注！" );
    }
}
else
{
    err( "本期尚未开盘！" );
}
if ( $prot_start == 0 )
{
    err( "尚未开放走飞补货功能！" );
}
$userid = $curruser['id'];
$acl = $curruser['acl'];
$previd = $curruser['prev_id'];
$rs = getdb( "select sum(amount) from bet2 where user_id=".$userid." and lotto_id=".$lotto_id." and rame=0" );
$usedamount = $rs->f[0] ? $rs->f[0] : 0;
$usedamount1 = $usedamount;
$rs->close();
$users = GetUserList( $userid );
if ( $users == "" )
{
    err( "没有会员！" );
}
if ( $acl == 2 )
{
    $u2 = get_user( $userid );
    $u3 = get_user( $u2['prev_id'] );
    $u4 = get_user( $u3['prev_id'] );
    $u5 = get_user( $u4['prev_id'] );
    $rs = getdb( "select sum(amount),sum(amount*mpercent/100) from bet where user_id in (".$users.") and lotto_id=".$lotto_id." and rame=0" );
}
else if ( $acl == 3 )
{
    $u2 = "";
    $u3 = get_user( $userid );
    $u4 = get_user( $u3['prev_id'] );
    $u5 = get_user( $u4['prev_id'] );
    $rs = getdb( "select sum(amount),sum(amount*percent2/100) from bet where user_id in (".$users.") and lotto_id=".$lotto_id." and rame=0" );
}
else if ( $acl == 4 )
{
    $u2 = "";
    $u3 = "";
    $u4 = get_user( $userid );
    $u5 = get_user( $u4['prev_id'] );
    $rs = getdb( "select sum(amount),sum(amount*percent3/100) from bet where user_id in (".$users.") and lotto_id=".$lotto_id." and rame=0" );
}
else if ( $acl == 5 )
{
    $u2 = "";
    $u3 = "";
    $u4 = "";
    $u5 = get_user( $userid );
    $rs = getdb( "select sum(amount),sum(amount*percent4/100) from bet where user_id in (".$users.") and lotto_id=".$lotto_id." and rame=0" );
}
$usedamount += $rs->f[0] ? $rs->f[0] : 0;
$usedamount2 = $rs->f[1] ? $rs->f[1] : 0;
$rs->close();
$maxMonTemp = 0;
$maxMonNumStr = "";
if ( $bettype == 9 )
{
    $rs = getdb( "select sum(amount) from bet2 where bettype in (9,33) and user_id=".$userid." and lotto_id=".$lotto_id." and rame=0" );
}
else if ( $bettype == 10 )
{
    $rs = getdb( "select sum(amount) from bet2 where bettype in (10,32) and user_id=".$userid." and lotto_id=".$lotto_id." and rame=0" );
}
else if ( $bettype == 29 )
{
    foreach ( $num as $val )
    {
        $val = str_pad( $val, 2, "0", STR_PAD_LEFT );
        $rs = getdb( "select sum(amount) from bet2 where bettype = 29 and user_id={$userid} and lotto_id={$lotto_id} and line_nums like '%{$val}%' and rame=0" );
        $maxMonTemp = max( $rs->f[0], $maxMonTemp );
        $maxMonNumStr = "(号码{$val})";
    }
}
else
{
    $rs = getdb( "select sum(amount) from bet2 where bettype=".$bettype." and user_id=".$userid." and lotto_id=".$lotto_id." and rame=0" );
}
$betamount = $rs->f[0] ? $rs->f[0] : 0;
$betamount = max( $betamount, $maxMonTemp );
$rs->close();
if ( $bettype == 9 )
{
    $rs = getdb( "select * from amounts where bettype in (9,33) and user_id=".$userid );
}
else if ( $bettype == 10 )
{
    $rs = getdb( "select * from amounts where bettype in (10,32) and user_id=".$userid );
}
else
{
    $rs = getdb( "select * from amounts where bettype=".$bettype." and user_id=".$userid );
}
$amount1 = $rs->f['amount1'];
$amount2 = $rs->f['amount2'];
$rs->close();
$usemaxamount = $curruser['usemaxamount'];
if ( $money < $betmin )
{
    err( "低於最小注额！" );
}
else if ( $amount1 < $money )
{
    err( "超过单注限额！" );
}
else if ( $amount2 < $betmoney + $betamount )
{
    err( "{$maxMonNumStr}超过单项限额！" );
}
else if ( $curruser['balance'] < $betmoney)
{
    err( "超过信用额度！" );
}
else if ( $usedamount2 < $betmoney + $usedamount1 )
{
    err( "超过总下注额！" );
}
else if ( $usemaxamount && $curruser['maxamount'] < $betmoney + $usedamount )
{
    err( "超过本期限额！" );
}
else if ( $stopbet <= date( "Hi" ) )
{
    err( "已经截止下注！" );
}
if ( $bettype == 29 )
{
    $rs = getdb( "select * from rate29 where lotto_id={$lotto_id} AND disk = 1 LIMIT 1" );
    if ( $rs->eof )
    {
        account_err( "系统目前维护中，请稍後！" );
    }
    $rate29Arr = array();
    foreach ( $num as $val )
    {
        $rate29Arr[] = floatval( $rs->f["r".d2( $val )] );
    }
    sort( $rate29Arr );
    $rate1 = $rate29Arr[0];
    $rate = $rate29Arr[0];
    if ( 0 < $auto_count )
    {
        sort( $num );
        $num1 = implode( ",", $num );
        $chk_mode = 0;
        $i = 0;
        while ( $i < $auto_count )
        {
            if ( $num1 == $auto_num[$i] )
            {
                ++$chk_mode;
            }
            ++$i;
        }
        if ( 0 < $chk_mode )
        {
            $rate1 -= $auto_down;
        }
    }
}
else if ( $bettype == 8 )
{
    $rs = getdb( "select * from rate8 where lotto_id={$lotto_id} AND disk = 1 LIMIT 1" );
    if ( $rs->eof )
    {
        account_err( "系统目前维护中，请稍後！" );
    }
    $rate8Arr = array();
    foreach ( $num as $val )
    {
        $rate8Arr[$val] = floatval( $rs->f["r".d2( $val )] );
    }
    asort( $rate8Arr );
    $idx = array_keys( $rate8Arr );
    $rate1 = $rate8Arr[$idx[0]];
    $rate = $rate8Arr[$idx[count( $rate8Arr ) - 1]];
}
else if ( $bettype == 48 )
{
    $rs = getdb( "select * from rate9 where lotto_id={$lotto_id} AND disk = 1 LIMIT 1" );
    if ( $rs->eof )
    {
        account_err( "系统目前维护中，请稍後！" );
    }
    $rate8Arr = array();
    foreach ( $num as $val )
    {
        $rate8Arr[$val] = floatval( $rs->f["r".d2( $val )] );
    }
    asort( $rate8Arr );
    $idx = array_keys( $rate8Arr );
    $rate1 = $rate8Arr[$idx[0]];
    $rate = $rate8Arr[$idx[count( $rate8Arr ) - 1]];
}
else
{
    $rs = getdb( "select * from rate5a where lotto_id=".$lotto_id );
    if ( $rs->eof )
    {
        err( "系统目前维护中，请稍後！" );
    }
    $rate1 = $rs->f["r".$bettype."_1"];
}
if ( $rate1 == 0 )
{
    err( "没有设置赔率！" );
}
$rate = $rate1;
if ( $bettype == 9 || $bettype == 10 || $bettype == 32 || $bettype == 33 )
{
    if ( $rs->f["r".$bettype."_2"] == 0 )
    {
        err( "赔率设置错误！" );
    }
    $rate = $rate1.",".$rs->f["r".$bettype."_2"];
}
$stime = systime();
if ( $count_num == 5 )
{
    $i = 0;
    while ( $i < $numcount - 4 )
    {
        $j = $i + 1;
        while ( $j < $numcount - 3 )
        {
            $k = $j + 1;
            while ( $k < $numcount - 2 )
            {
                $l = $k + 1;
                while ( $l < $numcount - 1 )
                {
                    $m = $l + 1;
                    while ( $m < $numcount )
                    {
                        $nums = explode( ",", $num[$i].",".$num[$j].",".$num[$k].",".$num[$l].",".$num[$m] );
                        sort( $nums );
                        save2bet2( $userid, $acl, $previd, $bettype, 1, implode( ",", $nums ), $money, $rate, $u2, $u3, $u4, $u5, $stime );
                        ++$m;
                    }
                    ++$l;
                }
                ++$k;
            }
            ++$j;
        }
        ++$i;
    }
}
elseif ( $count_num == 4 )
{
    $i = 0;
    while ( $i < $numcount - 3 )
    {
        $j = $i + 1;
        while ( $j < $numcount - 2 )
        {
            $k = $j + 1;
            while ( $k < $numcount - 1 )
            {
                $l = $k + 1;
                while ( $l < $numcount )
                {
                    $nums = explode( ",", $num[$i].",".$num[$j].",".$num[$k].",".$num[$l] );
					sort( $nums );
					save2bet2( $userid, $acl, $previd, $bettype, 1, implode( ",", $nums ), $money, $rate, $u2, $u3, $u4, $u5, $stime );
                    ++$l;
                }
                ++$k;
            }
            ++$j;
        }
        ++$i;
    }
}
elseif ( $count_num == 2 )
{
    $i = 0;
    while ( $i <= $numcount - 2 )
    {
        $j = $i + 1;
        while ( $j < $numcount )
        {
            $nums = explode( ",", $num[$i].",".$num[$j] );
            sort( $nums );
            $rpass = getline_number_rate( $bettype, $nums );
            if ( $rpass != false )
            {
                $rate1 = $rpass[0];
                $rate = $rpass[1];
            }
            if ( $bettype == 8 && count( $nums ) == 3 )
            {
                $rate = min( $rate8Arr[$nums[0]], $rate8Arr[$nums[1]] );
                $rate = min( $rate, $rate8Arr[$nums[2]] );
            }
            save2bet2( $userid, $acl, $previd, $bettype, 1, implode( ",", $nums ), $money, $rate, $u2, $u3, $u4, $u5, $stime );
            ++$j;
        }
        ++$i;
    }
}
$i = 0;
while ( $i <= $numcount - 2 )
{
    $j = $i + 1;
    while ( $j <= $numcount - 2 )
    {
        $k = $j + 1;
        while ( $k < $numcount )
        {
            $nums = explode( ",", $num[$i].",".$num[$j].",".$num[$k] );
            sort( $nums );
            if ( $bettype == 8 && count( $nums ) == 3 )
            {
                $rate = min( $rate8Arr[$nums[0]], $rate8Arr[$nums[1]] );
                $rate = min( $rate, $rate8Arr[$nums[2]] );
            }
            save2bet2( $userid, $acl, $previd, $bettype, 1, implode( ",", $nums ), $money, $rate, $u2, $u3, $u4, $u5, $stime );
            ++$k;
        }
        ++$j;
    }
    ++$i;
}
echo "<p>走飞成功！</p>";
echo "<p><input type=button value='回上一页' onclick='history.back()'></p>";
echo "<script>";
echo "setTimeout(\"location.href='op.php?op=agent&fp=bnow3'\",3000);";
echo "</script>";
?>